#!/bin/bash/python3
import os
from make_data.models.Component import DataWithOutRuleStrComponent
from make_data.models.Component import DataWithRuleStrComponent
from make_data.utils.FileUtils import FileUtils
from make_data.models.GenerateData import multi_process
import time

'''
    项目入口
'''
if __name__ == '__main__':

    r_rule = []
    data_str = ""
    data_rule = ""
    is_use_rule = input("是否使用规则文件造数：y/n ? ")
    if is_use_rule == 'y':
        read_path, rule_file_name = FileUtils.find_rule_file()
        r_rule = FileUtils(read_path + rule_file_name).read_rule()

        # 实际需创建字段数量
        column_num = int(r_rule[0])
        # 数据分隔符
        split_str = r_rule[1]
        # 截取字段选择规则数组
        column_rule = r_rule[2:int(len(r_rule))]
        # 截取字段类型规则数组
        column_type_rule = column_rule[::2]
        # 截取字段类型对应明细规则数组
        column_detail_rule = column_rule[1::2]
        data_str = DataWithRuleStrComponent(column_num, column_type_rule, column_detail_rule, split_str,
                                            data_str).get_data_str()
    else:
        column_num = input("请输入要造的假数据字段个数")
        data_rule += column_num + " "
        print(column_num)
        split_str: str = input("请输入分割符")
        data_rule, data_str = DataWithOutRuleStrComponent(column_num, data_rule, data_str,
                                                          split_str).get_data_with_rule_str()
        # 规则文件创建逻辑
        FileUtils.create_rule_file(data_rule)

    total_num = input("请输入您要创造的总数据条数")
    process_num = input("请输入造数并发数（多进程效率造数）")
    file_name = input("请输入您要保存的文件名，如果重复覆盖原文件请谨慎选择: ")
    path = os.getcwd() + os.sep + "make_data" + os.sep + "data" + os.sep  + "testData"
    file_type = input("请选择生成文件类型：1、 txt     2、 csv      3、 insert sql :")
    time_start = time.time()
    print("数据构建中，请耐心等待...")
    # 调用多进程造数方法
    multi_process(process_num, file_type, path, file_name, total_num, data_str, split_str)
    # 单进程模式
    # if file_type == "1":
    #     file_path = path + "\\" + file_name + ".txt"
    #     # 指定文件编码
    #     FileUtil.write_txt_file(file_path, total_num, data_str, split_str)
    # else:
    #     file_path = path + "\\" + file_name + ".csv"
    #     FileUtil.write_csv_file(file_path, total_num, data_str, split_str)

    time_end = time.time()
    print('构建完成，总耗时（s）：', time_end - time_start)
